﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using IronBrokers.ClientManager.Model;
using IronBrokers.ClientManager.Database;
using RabbitMQ.Client;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using IronBrokers.ClientManager.Properties;

namespace IronBrokers.ClientManager
{
    public class ClientManagerService : ServiceBase, IClientManagerService
    {
        #region Publish
        public Response Publish(StockData updateData)
        {
            try
            {
                PersistenceManager.UpdateStockValue(updateData);
                this.PublishToQueue(updateData);
                return new Response();
            }
            catch (Exception ex)
            {
                // TODO: log error
                return new Response(ex);
            }
        }

        #endregion

        #region PublishToQueue

        private void PublishToQueue(StockData updateData)
        {
            ConnectionFactory factory = new ConnectionFactory();
            factory.Uri = Settings.Default.ExchangeAddress;
            using (IConnection connection = factory.CreateConnection())
            {
                using (IModel model = connection.CreateModel())
                {
                    model.ExchangeDeclare(Settings.Default.ExchangeName, ExchangeType.Topic, true);
                    String queueName = String.Format("{0}_Queue", Settings.Default.ExchangeName);
                    model.QueueDeclare(queueName, true, false, false, null);
                    model.QueueBind(queueName, Settings.Default.ExchangeName, "#", null);

                    MemoryStream memStream = new MemoryStream();
                    BinaryFormatter formatter = new BinaryFormatter();
                    formatter.Serialize(memStream, updateData);
                    byte[] objectBytes = memStream.ToArray();

                    model.BasicPublish(Settings.Default.ExchangeName, updateData.GoodName, null, objectBytes);
                }
            }
        } 

        #endregion

        #region RegisterUser
        public Response RegisterUser(string userName, string password)
        {
            try
            {
                PersistenceManager.RegisterUser(userName, password);
                return new Response();
            }
            catch (Exception ex)
            {
                // TODO: log error
                return new Response(ex);
            }
        } 
        #endregion

        #region UnregisterUser
        public Response UnregisterUser(string userName)
        {
            try
            {
                PersistenceManager.UnregisterUser(userName);
                return new Response();
            }
            catch (Exception ex)
            {
                // TODO: log error
                return new Response(ex);
            }
        } 
        #endregion

        #region SetUserPriviledges
        public Response SetUserPriviledges(string userName, List<string> priviledges)
        {
            try
            {
                PersistenceManager.SetUserPriviledges(userName, priviledges);
                return new Response();
            }
            catch (Exception ex)
            {
                // TODO: log error
                return new Response(ex);
            }
        } 
        #endregion

        #region GetUserList
        public UserListResponse GetUserList()
        {
            try
            {
                List<Model.User> users = PersistenceManager.GetUserList();
                return new UserListResponse(users);
            }
            catch (Exception ex)
            {
                // TODO: log error
                return new UserListResponse(ex);
            }
        } 
        #endregion
    }
}
